<template>
    <h2>等级权益分配</h2>
    当前等级是:<span style="color: red;">{{ Linfo.levelname }}</span>
    &nbsp;&nbsp;&nbsp;<input type="button" value="配置" class="btn btn-success" @click="Add" />
    <table class="table table-border">
        <thead>
            <tr class="bg-info">
                <td> <input type="checkbox" v-model="checkall" @change="CheckAll"/> </td>
                <td>权益名称</td>
            </tr>
        </thead>
        <tbody>
            <tr v-for="item in Qinfo">
                <td> <input type="checkbox" :checked="checkone" :value="item.qid" v-model="arrids" @change="CheckOne"/> </td>
                <td>{{ item.qname }}</td>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import { useRouter,useRoute } from 'vue-router';
const router=useRouter()
const route=useRoute()
const Qinfo=ref([{
    qid:0,
    qname:'',
    image:'',
    state:true
}])
const Linfo=ref({
    levelname:''
})
onMounted(()=>{
    QShow()
    LShow()
})
//显示
const QShow=()=>{
    axios.get('https://localhost:7279/api/Quanyi/QuanyiShow')
    .then(res=>{
        Qinfo.value=res.data
    })
    .catch(err=>{
        console.log(err)
    })
}
const LShow=()=>{
    axios.get('https://localhost:7279/api/LevelGetLevel',{
        params:{
            id:route.params.id
        }
    })
    .then(res=>{
        Linfo.value=res.data
    })
}
//全选全不选
const checkall=ref(false)
const checkone=ref(false)
const arrids:any=ref([])

//全选
const CheckAll=()=>{
    if(checkall.value==true){
        checkone.value=true;
        arrids.value=Qinfo.value.map((item:any)=>{
            return item.qid
        })
    }
    else{
        checkone.value=false;
        arrids.value=[]
    }
}
//反选
const CheckOne=()=>{
    if(Qinfo.value.length==arrids.value.length){
        checkall.value=true
    }   
    else{
        checkall.value=false
    }
    console.log(arrids.value.toString())
}

//配置权限
const Add=()=>{
    

    axios.get('https://localhost:7279/api/LevelLQtableAdd',{
        params:{
            lid:route.params.id,
            qids:arrids.value.toString()
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('分配成功')
        }
        else{
            alert('分配失败')
        }
    })
}



</script>
